package com.jiangjiang.eshop.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.order.constant.OrderStatus;
import com.jiangjiang.eshop.order.convert.OrderInfoConvert;
import com.jiangjiang.eshop.order.dao.OrderInfoMapper;
import com.jiangjiang.eshop.order.domain.OrderInfoEntity;
import com.jiangjiang.eshop.order.dto.OrderInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName OrderInfoService
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/22 11:30
 * @Version 1.0
 **/
@Service
public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEntity> {

    @Autowired
    private OrderInfoConvert orderInfoConvert;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private OrderOperateLogService orderOperateLogService;
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    /**
     * 保存订单主表信息
     * @param orderInfo
     */
    public Long saveOrderInfo(OrderInfoDTO orderInfo) {
        OrderInfoEntity orderInfoEntity = orderInfoConvert.convertDTO2Entity(orderInfo);
        this.save(orderInfoEntity);
        return orderInfoEntity.getId();
    }

    /**
     * 保存订单主表信息
     *
     * @param id 订单主表id
     */

    public OrderInfoDTO queryById(Long id) {
        OrderInfoDTO order = orderInfoConvert.convertEntity2DTO(this.getById(id));
        // 补充订单明细数据
        order.setOrderItems(orderItemService.queryByOrderInfoId(order.getId()));
        // 补充订单操作日志数据
        order.setLogs(orderOperateLogService.queryByOrderInfoId(order.getId()));
        return order;
    }

    /**
     * 更新订单状态
     * @param id 订单id
     * @param state 状态
     */
    public void updateState(Long id, Integer state) {
        LambdaUpdateWrapper<OrderInfoEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(OrderInfoEntity::getId, id)
                .set(OrderInfoEntity::getOrderStatus, state)
                // 当状态流转到完成状态时候，需要记录确认收货时间
                .set(OrderStatus.FINISHED.equals(state), OrderInfoEntity::getConfirmReceiptTime, new Date());
        this.update(wrapper);
    }


    /**
     * 查询所有未支付的订单数据
     * @return
     */
    public List<OrderInfoDTO> queryAllUnPayed() {
        return this.queryByStatus(OrderStatus.WAIT_FOR_PAY);
    }

    /**
     * 查询所有未收货的订单数据
     * @return
     */
    public List<OrderInfoDTO> queryAllUnReceive() {
        return this.queryByStatus(OrderStatus.WAIT_FOR_RECEIVE);
    }

    /**
     * 查询所有未支付的订单数据
     * @param status 订单状态
     * @return
     */
    public List<OrderInfoDTO> queryByStatus(Integer status) {
        LambdaQueryWrapper<OrderInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(OrderInfoEntity::getOrderStatus, status);
        return orderInfoConvert.convertEnties2DTOList(this.list(wrapper));
    }


    /**
     * 更新订单数据
     * @param order 订单数据
     */
    public void updateOrderInfo(OrderInfoDTO order) {
        this.updateById(orderInfoConvert.convertDTO2Entity(order));

    }

    /**
     * 查询确认收货时间超过了7天而且还没有发表评论的订单
     * @return
     */
    public List<OrderInfoDTO> listNotPublishedCommentOrders() {
        List<OrderInfoDTO> orderInfo = orderInfoConvert.convertEnties2DTOList(orderInfoMapper.listNotPublishedCommentOrders());
        for (OrderInfoDTO order : orderInfo) {
            // 补充订单明细数据
            order.setOrderItems(orderItemService.queryByOrderInfoId(order.getId()));
            // 补充订单操作日志数据
            order.setLogs(orderOperateLogService.queryByOrderInfoId(order.getId()));
        }
        return orderInfo;
    }
}
